<template>
  <client-only>
    <div class="body">
      <div class="box">
        <div class="search">
          <input
            type="text"
            placeholder="输入游戏名称"
            v-model.trim="searchVal"
            @keydown.enter="searchGoods"
          />
          <button @click="searchGoods">搜索</button>
          <div>
            <span>排序依据</span>
            <select name="" id="" v-model="sortType">
              <option value="0">发行日期</option>
              <option value="1">价格从低到高</option>
              <option value="2">价格从高到低</option>
            </select>
          </div>
        </div>
        <div id="container">
          <div class="left">
            <a
              class="left_content"
              :class="{ leftHov: gameList.id == item.id }"
              v-for="item in partGame"
              :href="`/gameCategory/${item.id}`"
              :key="item.id"
            >
              <img :src="item.firstImg" :alt="item.name" width="20%" />
              <div class="left_font">
                <div
                  class="font_tit"
                  :class="{ leftTitHov: gameList.id == item.id }"
                >
                  {{ item.name }}
                </div>
                <img
                  src="http://www.xclove.xyz/gameMallImg/icon_platform_win_dark.png"
                  style="opacity: 0.3; margin: 5px 0"
                  alt=""
                />
                <div>
                  <span
                    v-for="(smTit, index) in item.tab"
                    :key="smTit"
                    class="smContent"
                    >{{ smTit
                    }}<span v-if="index + 1 != item.tab.length">，</span>
                  </span>
                </div>
              </div>
              <div class="date">{{ item.issueDate }}</div>
              <div class="price">￥{{ item.price.toFixed(2) }}</div>
            </a>
          </div>
        </div>
      </div>
    </div>
  </client-only>
</template>
<script>
import gameInfoApi from "@/api/gameInfo";
import Cookies from "js-cookie";
export default {
  data() {
    return {
      gameList: [], //存放全部数据
      partGame: [], //所展示的商品信息
      searchVal: "", //搜索框内容
      sortType: "0", //监视下拉框的选项
    };
  },
  created() {
    // 拿到所有的数据
    this.init();
  },
  watch: {
    searchVal: function (newVal, oldVal) {
      newVal = newVal.toLocaleLowerCase();
      Cookies.set("gameName", newVal);
      this.searchGoods();
    },
    sortType: function () {
      // 对商品进行排序
      this.sortGoods();
    },
  },
  methods: {
    // 初始化游戏列表
    init() {
      gameInfoApi.getGameList().then((res) => {
        // 将商品数据存储
        this.gameList = res.data;
        // 当页面跳转过来时获取搜索信息
        let searchVal = Cookies.get("gameName").toLocaleLowerCase();
        // 如果是空值就显示所有数据，否则就显示对应的数据
        if (searchVal) {
          this.searchVal = searchVal;
          this.searchGoods();
        } else {
          // 当为搜索框空时显示所有商品
          this.partGame = this.gameList;
        }
        // 对商品进行排序
        this.sortGoods();
      });
    },
    // 搜索商品
    searchGoods() {
      // 筛选一下找出哪个符合
      this.partGame = this.gameList.filter((item) =>
        item.name
          .toLocaleLowerCase()
          .includes(this.searchVal.toLocaleLowerCase())
      );
      // 对商品进行排序
      this.sortGoods();
    },
    // 对商品进行排序
    sortGoods() {
      switch (this.sortType) {
        case "0":
          this.partGame.sort((a, b) => {
            // console.log(new Date(Date.parse(a.issueDate.replace(/-/g, "/"))));
            return (
              new Date(Date.parse(b.issueDate.replace(/-/g, "/"))) -
              new Date(Date.parse(a.issueDate.replace(/-/g, "/")))
            );
          });
          break;
        case "1":
          this.partGame.sort((a, b) => a.price - b.price);
          break;
        case "2":
          this.partGame.sort((a, b) => b.price - a.price);
          break;
        default:
          break;
      }
    },
    // 跳转到详情页
    goInfo(gameId) {
      this.$router.push(`/gameCategory/${gameId}`);
    },
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
a:hover {
  text-decoration: none;
}
.body {
  background-color: #1b2838;
}

.box {
  width: 75%;
  margin: 0 auto;
  padding-top: 10px;
}

.search {
  background-color: #111822;
  padding: 10px;
  margin-bottom: 10px;
}

.search > div {
  float: right;
}

.search input {
  width: 400px;
  height: 28px;
  border: 0;
  border-radius: 5px;
  background-color: #233a4c;
  color: white;
  padding-left: 15px;
}

.search button {
  width: 50px;
  height: 28px;
  border: 0;
  border-radius: 5px;
  background-color: #233a4c;
  color: #88bde5;
  margin-top: 3px;
}

.search div span {
  color: #606e89;
  font-size: 14px;
}

.search div select {
  width: 130px;
  background-color: #192b39;
  border: 0;
  border-radius: 5px;
  height: 26px;
  color: #88bde5;
}

#container {
  display: flex;
}
#container .left {
  width: 90%;
  margin: 0 auto;
}

#container .left_content {
  width: 100%;
  display: flex;
  background: rgba(0, 0, 0, 0.2);
  margin-bottom: 10px;
  transition: 0.1s;
  color: #384959;
  position: relative;
}
#container .left_content:hover {
  background-color: rgba(165, 203, 223, 0.8) !important;
}

#container .left_content .left_font {
  width: 350px;
  box-sizing: border-box;
  padding: 1% 10px;
  font-size: 12px;
}
#container .left_content .date {
  position: absolute;
  top: 50%;
  left: 60%;
  transform: translate(-50%, -50%);
  font-size: 16px;
}

#container .left_content .price {
  width: 80px;
  text-align: right;
  color: #fff;
  font-size: 15px;
  position: absolute;
  top: 50%;
  right: 100px;
  transform: translateY(-50%);
}

#container .left_content .left_font .font_tit {
  color: #c7d5e0;
  font-size: 15px !important;
}
</style>